package com.gxa.k12.auth.controller;


import com.gxa.k12.auth.entity.UserEntity;
import com.gxa.k12.auth.service.SmsService;
import com.gxa.k12.auth.service.UserService;
import com.gxa.k12.auth.utils.JsonResult;
import com.gxa.k12.common.exception.BusinessException;
import com.gxa.k12.common.util.PageUtils;
import com.gxa.k12.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

/**
 * 用户表
 *
 * @author wenjie
 * @email wenj73487@gmail.com
 * @date 2021-07-20 11:28:11
 */
@RestController
@RequestMapping("course/user")
@Api(value = "这是权限验证的接口,包含登录，注册，一键登录",tags = "这是权限验证的接口,包含登录，注册，一键登录")
public class UserController {
    @Autowired
    private UserService userService;


    @Resource
    private SmsService smsService;

    @PostMapping(value = "/loginGetCode")
    @ApiOperation(value = "发送一键登录短信",notes = "发送一键登录短信",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户手机号",name = "phone",required = true,paramType = "query",dataType = "string"),
            @ApiImplicitParam(value = "密码",name = "password",required = true,paramType = "query",dataType = "string")
    })
    public JsonResult sendLoginSms(@RequestParam("phone") String phone , @RequestParam("password") String password) throws BusinessException {
        if(!phone.matches("^[1](([3][0-9])|([4][5,7,9])|([5][0-9])|([6][6])|([7][3,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$")){
            throw new BusinessException("手机号码格式不合法");
        }

        smsService.sendLoginSms(phone,password);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("短信发送成功");
        return  jsonResult;

    }
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户对象",name = "userEntity",required = true,paramType = "body",dataType = "UserEntity")
    })
    @PostMapping(value = "/register")
    @ApiOperation(value = "注册",notes = "注册",httpMethod = "POST")
    public JsonResult register(@RequestBody UserEntity userEntity){

        Map<String, Object> register = userService.register(userEntity);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(register);

        return jsonResult;
    }


    @PostMapping(value = "/login")
    @ApiOperation(value = "登录",notes = "登录",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户手机",name = "phone",required = true,paramType = "query",dataType = "string"),
            @ApiImplicitParam(value = "短信验证码",name = "smscode",required = true,paramType = "query",dataType = "string")
    })
    public JsonResult login(@RequestParam("phone") String phone,@RequestParam("smscode")  String smscode){
        Map<String, String> login = userService.login(phone, smscode);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(login);

        return jsonResult;
    }





}
